package com.sky.controller.admin;

import com.sky.dto.DishPageQueryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐管理
 */

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetMealController {

    @Autowired
    private SetMealService setMealService;

    /**
     * 套餐的条件分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("套餐分页查询，参数为{}", setmealPageQueryDTO);
        PageResult pageResult = setMealService.pageQuery(setmealPageQueryDTO);
        log.info("查询到的分页信息：{}", pageResult);
        return Result.success(pageResult);
    }

    /**
     * 套餐售卖状态的修改
     *
     * @param status
     * @return
     */
    @CacheEvict(cacheNames = "setMeals_categoryId",allEntries = true)
    @PostMapping("/status/{status}")
    public Result setSetMealStatus(@PathVariable Integer status, Long id) {
        setMealService.setSetMealStatus(status, id);
        return Result.success();
    }

    /**
     * 套餐信息的修改
     *
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @CacheEvict(cacheNames = "setMeals_categoryId",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐的信息，参数为：{}", setmealDTO);
        setMealService.update(setmealDTO);
        return Result.success();
    }

    /**
     * 根据id查询套餐(修改套餐时的数据回显)
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result selectSetMealById(@PathVariable Long id) {
        log.info("根据id查询套餐(修改套餐时的数据回显)，参数为id = {}", id);
        SetmealVO setmealVO = setMealService.selectSetMealById(id);
        return Result.success(setmealVO);
    }

    /**
     * 新增套餐
     *
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @CacheEvict(cacheNames = "setMeals_categoryId",key = "#setmealDTO.categoryId")
    public Result insertSetMeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增菜品，参数为{}", setmealDTO);
        setMealService.insertSetMeal(setmealDTO);
        return Result.success();
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "setMeals_categoryId",allEntries = true)
    public Result deleteSetMeals(@RequestParam List<Long> ids) {
        log.info("批量删除套餐 ,id为{}", ids);
        setMealService.deleteSetMeals(ids);
        return Result.success();
    }


}
